<?php
/**
 *    商品管理控制器
 */
class GoodsApp extends BackendApp
{
    var $_goods_mod;
    var $_store_detail_mod;

    function __construct()
    {
        $this->GoodsApp();
    }
    function GoodsApp()
    {
        parent::BackendApp();

        $this->_goods_mod =& m('goods');
        $this->_store_detail_mod = &m("store_detail");
    }

    /* 商品列表 */
    function index()
    {
        $conditions = $this->_get_query_conditions(array(
            array(
                'field' => 'goods_name',
                'equal' => 'like',
            ),
            array(
                'field' => 'store_name',
                'equal' => 'like',
            ),
            array(
                'field' => 'brand',
                'equal' => 'like',
            ),array(
                'field' => 'gst.views',
                'name'  => 'views_from',
                'equal' => '>=',
                'type'  => 'numeric',
            ),array(
                'field' => 'gst.views',
                'name'  => 'views_to',
                'equal' => '<=',
                'type'  => 'numeric',
            ),
        ));

        // 分类
        $cate_id = empty($_GET['cate_id']) ? 0 : intval($_GET['cate_id']);
        if ($cate_id > 0)
        {
            $cate_mod =& bm('gcategory');
            $cate_ids = $cate_mod->get_descendant_ids($cate_id);
            $conditions .= " AND cate_id" . db_create_in($cate_ids);
        }
        
        //更新排序
        if (isset($_GET['sort']) && isset($_GET['order']))
        {
            $sort  = strtolower(trim($_GET['sort']));
            $order = strtolower(trim($_GET['order']));
            if (!in_array($order,array('asc','desc')))
            {
             $sort  = 'goods_id';
             $order = 'desc';
            }
        }
        else
        {
            $sort  = 'goods_id';
            $order = 'desc';
        }
        $goodsSpec = & m('goodsspec');
        $storeStock = & m('storestock');

        //年代
        if($_GET['times']){
            $times = explode(',',$_GET['times']);
            $sql_time = '';
            foreach($times as $k=>$times_item){
                if($k == 0){
                    $sql_time = 'g.times like '."'%".trim($times_item)."%'";
                }else{
                    $sql_time .= ' OR g.times like '."'%".trim($times_item)."%'";
                }
            }
            $conditions .= " AND (".$sql_time.")";
        }
        //标签
        if($_GET['screen_label']){
            $conditions .= ' AND g.screen_label	 like '."'%".$_GET['screen_label']."%'";
        }

        //仓库 、国际码
        if($_GET['storestock_id'] || $_GET['international_code']){
            if($_GET['storestock_id'] && is_numeric($_GET['storestock_id'])){
                $where['conditions'] = 'spec_3value = '.$_GET['storestock_id'];
                $this->assign('storestock_id',$_GET['storestock_id']);
            }
            if($_GET['international_code']){
                $where['conditions'] = 'international_code like '."'%".$_GET['international_code']."%'";
                $this->assign('international_code',$_GET['international_code']);
            }
            if($_GET['storestock_id'] && is_numeric($_GET['storestock_id']) && $_GET['international_code']){
                $where['conditions'] = 'spec_3value = '.$_GET['storestock_id'].' AND international_code like'."'%".$_GET['international_code']."%'";
                $this->assign('storestock_id',$_GET['storestock_id']);
                $this->assign('international_code',$_GET['international_code']);
            }
            $goodsSpecInfo = $goodsSpec->find($where);
            $specList = array_values($goodsSpecInfo);
            $ids = '';
            if(!empty($specList)){
                foreach($specList as $k=>$v){
                    if($k == 0){
                        $ids = $v['goods_id'];
                    }else{
                        $ids .= ','.$v['goods_id'];
                    }
                }
                $conditions .= ' AND g.goods_id IN ('.$ids.')';
            }else{
                $conditions .= ' AND g.goods_id = 0 ';
            }
        }

        //是否选择心愿单筛选
        if($_GET['wish_close']){
            if($_GET['wish_close'] == 2){
                $wish_close = 0;
            }else{
                $wish_close = 1;
            }
            $conditions .= ' AND g.wish_close = '.$wish_close;

            $this->assign('wish_close',$_GET['wish_close']);
        }
        //是否特卖
        if($_GET['sales']){
            if($_GET['sales'] == 2){
                $sales = 0;
            }else{
                $sales = 1;
            }
            $conditions .= ' AND g.sales = '.$sales;

            $this->assign('sales',$_GET['sales']);
        }
        //是否上下架
        if($_GET['closed']){
            if($_GET['closed'] == 2){
                $closed = 0;
            }else{
                $closed = 1;
            }
            $conditions .= ' AND g.closed = '.$closed;
            $this->assign('closed',$_GET['closed']);
        }
        $page = $this->_get_page(100);
        $goods_list = $this->_goods_mod->get_list(array(
            'conditions' => "1 = 1" . $conditions,
            'count' => true,
            'order' => "$sort $order",
            'limit' => $page['limit'],
        ));

        $goodsSpec = &m('goodsspec');
        foreach ($goods_list as $key => $goods)
        {
            $goods_list[$key]['cate_name'] = $this->_goods_mod->format_cate_name($goods['cate_name']);
            $goods_list[$key]['default_image']  =   WEB_PHOTO_SRC . "/" . $goods['default_image'];
            $goods_list[$key]['discount'] = $goods['discount'] * 10;
            $goods_list[$key]['label'] = $goods['label']?$goods['label']:'无';
            $goods_list[$key]['screen_label'] = $goods['screen_label']?$goods['screen_label']:'无';
            if($goods['discount'] == 1.00){
                $goods_list[$key]['discount'] = '不打';
            }
            $retult = $goodsSpec->find(array('conditions'=>"goods_id = ".$key));

            //声明一个库存数
            $inventory = $this->_store_detail_mod->get_count($goods['sku']);

            //总库存
            $goods_list[$key]['total_inventory'] = $inventory?$inventory:0;
        }

        $this->assign('goods_list', $goods_list);
        $page['item_count'] = $this->_goods_mod->getCount();
        $this->_format_page($page);
        $this->assign('page_info', $page);

        //仓库
        $this->assign('storeStock',$storeStock->get_options());

        // 第一级分类
        $cate_mod =& bm('gcategory', array('_store_id' => 0));
        $this->assign('gcategories', $cate_mod->get_options(0, true));
        $this->import_resource(array('script' => 'mlselection.js,inline_edit.js'));
        $this->display('goods.index.html');
    }
    
//    lichen 2019/6/26 start
    /* 商品iframe列表 */
    /**
     *
     */
    function iframe()
    {
        $conditions = $this->_get_query_conditions(array(
            array(
                'field' => 'goods_name',
                'equal' => 'like',
            ),
            array(
                'field' => 'store_name',
                'equal' => 'like',
            ),
            array(
                'field' => 'brand',
                'equal' => 'like',
            ),

        ));
        // 分类
        $cate_id = empty($_GET['cate_id']) ? 0 : intval($_GET['cate_id']);
        if ($cate_id > 0)
        {
            $cate_mod =& bm('gcategory');
            $cate_ids = $cate_mod->get_descendant_ids($cate_id);
            $conditions .= " AND cate_id" . db_create_in($cate_ids);
        }

        //更新排序
        if (isset($_GET['sort']) && isset($_GET['order']))
        {
            $sort  = strtolower(trim($_GET['sort']));
            $order = strtolower(trim($_GET['order']));
            if (!in_array($order,array('asc','desc')))
            {
                $sort  = 'goods_id';
                $order = 'desc';
            }
        }
        elseif($_GET['sort'] == 4){ //今日上新
                $sort = 'add_time';
                $order = 'desc';
        }else {
            $sort  = 'goods_id';
            $order = 'desc';
        }
        $page = $this->_get_page(60);
        //断码

        //查询断码价
        $spec_model = & m('goodsspec');

        if($_GET['cut_off']){
            $sql = "select 
            ecm_storestock.stock_name, 
            ecm_goods.goods_id, 
            group_concat(ecm_goods_spec.spec_2) as spec2, 
            case when locate(',',group_concat(ecm_goods_spec.spec_2)) > 0 then 0 else 1 end as is_unique,
            ecm_goods_spec.stock
        from 
            ecm_goods, 
            ecm_goods_spec, 
            ecm_storestock 
        where 
            ecm_storestock.storestock_id = ecm_goods_spec.spec_3value and ecm_goods.goods_id = ecm_goods_spec.goods_id and ecm_goods_spec.stock > 0
        group by 
            ecm_goods.goods_id 
        having is_unique > 0";
            $db = &db('goods');
            $res_sql = $db->getall($sql);
            $conditions .= " AND g.goods_id" . db_create_in(array_column($res_sql,'goods_id'));
            var_dump($conditions);
        }

        $goodsSpec = & m('goodsspec');
        $storeStock = & m('storestock');

        //年代
        if($_GET['times']){
            $times = explode(',',$_GET['times']);
            $sql_time = '';
            foreach($times as $k=>$times_item){
                if($k == 0){
                    $sql_time = 'g.times like '."'%".trim($times_item)."%'";
                }else{
                    $sql_time .= ' OR g.times like '."'%".trim($times_item)."%'";
                }
            }
            $conditions .= " AND (".$sql_time.")";
        }

        //仓库 、国际码
        if($_GET['storestock_id'] || $_GET['international_code']){
            if($_GET['storestock_id'] && is_numeric($_GET['storestock_id'])){
                $where['conditions'] = 'spec_3value = '.$_GET['storestock_id'];
                $this->assign('storestock_id',$_GET['storestock_id']);
            }
            if($_GET['international_code']){
                $where['conditions'] = 'international_code like '."'%".$_GET['international_code']."%'";
                $this->assign('international_code',$_GET['international_code']);
            }
            if($_GET['storestock_id'] && is_numeric($_GET['storestock_id']) && $_GET['international_code']){
                $where['conditions'] = 'spec_3value = '.$_GET['storestock_id'].' AND international_code like'."'%".$_GET['international_code']."%'";
                $this->assign('storestock_id',$_GET['storestock_id']);
                $this->assign('international_code',$_GET['international_code']);
            }
            $goodsSpecInfo = $goodsSpec->find($where);
            $specList = array_values($goodsSpecInfo);
            $ids = '';
            if(!empty($specList)){
                foreach($specList as $k=>$v){
                    if($k == 0){
                        $ids = $v['goods_id'];
                    }else{
                        $ids .= ','.$v['goods_id'];
                    }
                }
                $conditions .= ' AND g.goods_id IN ('.$ids.')';
            }else{
                $conditions .= ' AND g.goods_id = 0 ';
            }
        }

        //是否上下架
        if($_GET['closed']){
            if($_GET['closed'] == 2){
                $closed = 0;
            }else{
                $closed = 1;
            }
            $conditions .= ' AND g.closed = '.$closed;
            $this->assign('closed',$_GET['closed']);
        }

        //库存数筛选
        if($_GET['count_from'] || $_GET['count_to']){
            if($_GET['count_from'] && !$_GET['count_to']){
                $count_from = $_GET['count_from'];
                $sql = "select * from (select sum(stock) as ccaa,goods_id from ecm_goods_spec group by goods_id ) as ccaa where ccaa >=$count_from";
            }elseif($_GET['count_to'] && !$_GET['count_from']){
                $count_to = $_GET['count_to'];
                $sql = "select * from (select sum(stock) as ccaa,goods_id from ecm_goods_spec group by goods_id ) as ccaa where ccaa <=$count_to";
            }else{
                $count_to = $_GET['count_to'];
                $count_from = $_GET['count_from'];
                $sql = "select * from (select sum(stock) as ccaa,goods_id from ecm_goods_spec group by goods_id ) as ccaa where (ccaa >=$count_from and ccaa<=$count_to)";
            }
            $info = $goodsSpec->db->query($sql);
            while ($chat = $goodsSpec->db->fetchRow($info)) {
                $chat_listOld[] = $chat;
            }
            if(!empty($chat_listOld)){
                $wish_ids = '';
                foreach($chat_listOld as $k=>$v){
                    if($k == 0){
                        $wish_ids = $v['goods_id'];
                    }else{
                        $wish_ids .= ','.$v['goods_id'];
                    }
                }
                $conditions .= " AND g.goods_id in (".$wish_ids.")";
            }
            $this->assign('count_from',$_GET['count_from']);
            $this->assign('count_to',$_GET['count_to']);
        }

        //价格筛选
        if($_GET['price_from'] || $_GET['price_to']){
            if($_GET['price_from'] && !$_GET['price_to']){
                $price_from = $_GET['price_from'];
                $sql = "select * from (select price as ccaa,goods_id from ecm_goods_spec group by goods_id ) as ccaa where ccaa >=$price_from";
            }elseif($_GET['price_to'] && !$_GET['price_from']){
                $price_to = $_GET['price_to'];
                $sql = "select * from (select price as ccaa,goods_id from ecm_goods_spec group by goods_id ) as ccaa where ccaa <=$price_to";
            }else{
                $price_to = $_GET['price_to'];
                $price_from = $_GET['price_from'];
                $sql = "select * from (select price as ccaa,goods_id from ecm_goods_spec group by goods_id ) as ccaa where (ccaa >=$price_from and ccaa<=$price_to)";
            }
            $info_price = $goodsSpec->db->query($sql);
            while ($chat_price = $goodsSpec->db->fetchRow($info_price)) {
                $chat_listOld_price[] = $chat_price;
            }
            if(!empty($chat_listOld_price)){
                $wish_ids_price = '';
                foreach($chat_listOld_price as $k=>$v){
                    if($k == 0){
                        $wish_ids_price = $v['goods_id'];
                    }else{
                        $wish_ids_price .= ','.$v['goods_id'];
                    }
                }
                $conditions .= " AND g.goods_id in (".$wish_ids_price.")";
            }
            $this->assign('price_from',$_GET['price_from']);
            $this->assign('price_to',$_GET['price_to']);
        }

        $goods_list = $this->_goods_mod->get_list(array(
            'conditions' => "1 = 1" . $conditions,
            'count' => true,
            'order' => "$sort $order",
            'limit' => $page['limit'],
        ));
        foreach ($goods_list as $key => $goods)
        {
            $goods_list[$key]['cate_name'] = $this->_goods_mod->format_cate_name($goods['cate_name']);
            $goods_list[$key]['default_image']  =   WEB_PHOTO_SRC . "/" . $goods['default_image'];
            $goods_list[$key]['times']  =   $goods['times'];
//            //单独查询规格表中的标价
            $retult = $goodsSpec->find(array('conditions'=>"goods_id = ".$key));
            if(!empty($retult)) {
                $str['domestic_price'] = 0;
                $str['domestic_discount'] = '无';
                $str['hong_kong_price'] = 0;
                $str['hong_kong_discount'] = '无';

                //声明一个库存数
                $inventory = $this->_store_detail_mod->get_count($goods['sku']);

                $in_array = array();
                $stock_array = array();
                foreach ($retult as $k => $v) {
                    if ($v['spec_3value'] == 25) {                    //国内仓标价
                        $str['domestic_price'] = $v['price'];
                        if($goods['market_price'] && $goods['market_price'] != 0.00){
                            $str['domestic_discount'] = sprintf('%.2f', $v['price'] / $goods['market_price']) . '折';//国内仓折扣
                        }else{
                            $str['domestic_discount'] = 0 . '折';//国内仓折扣
                        }
                    }
                    if ($v['spec_3value'] == 26) {
                        $str['hong_kong_price'] = $v['price'];
                        if($v['price'] && $v['price'] != 0.00 && $goods['market_price'] != 0.00){
                            $str['hong_kong_discount'] = sprintf('%.2f', $v['price'] / $goods['market_price']) . '折';//香港折扣
                        }else{
                            $str['hong_kong_discount'] = 0 . '折';//香港折扣
                        }
                    }
                }
                $res_ss = $goodsSpec->getSpecStockInfo($key);
                foreach ($res_ss as $k => $v) {
                    $v['international_code'] && !in_array($v['international_code'],$in_array)  ?  $in_array[] = $v['international_code'] : NULL;
                    $v['stock_name'] && !in_array($v['stock_name'],$stock_array) ? $stock_array[] = $v['stock_name'] : NULL;


                }
                $in_array ? $goods_list[$key]['international_code'] = implode('<br>',$in_array) : NULL;
                $stock_array ? $goods_list[$key]['stock_name'] = implode('<br>',$stock_array) : NULL;
                $goods_list[$key]['domestic_price'] = $str['domestic_price'];
                $goods_list[$key]['domestic_discount'] = $str['domestic_discount'];
                $goods_list[$key]['hong_kong_price'] = $str['hong_kong_price'];
                $goods_list[$key]['hong_kong_discount'] = $str['hong_kong_discount'];
                //总库存
                $goods_list[$key]['total_inventory'] = $inventory?$inventory:0;

            }
        }

        $this->assign('goods_list', $goods_list);

        $page['item_count'] = $this->_goods_mod->getCount();
        $this->_format_page($page);
        $this->assign('page_info', $page);

        // 第一级分类
        $cate_mod =& bm('gcategory', array('_store_id' => 0));
        $this->assign('gcategories', $cate_mod->get_options(0, true));

        //仓库
        $this->assign('storeStock',$storeStock->get_options());

        $this->assign('cut_off', $_GET['cut_off']? 1 : 0); //是否有查询条件
        $this->import_resource(array('script' => 'mlselection.js,inline_edit.js '));
        $this->display('goods.iframe.html');
    }
//    lichen 2019/6/26 end
    /* 推荐商品到 */
    function recommend()
    {
        if (!IS_POST)
        {
            /* 取得推荐类型 */
            $recommend_mod =& bm('recommend', array('_store_id' => 0));
            $recommends = $recommend_mod->get_options();
            if (!$recommends)
            {
                $this->show_warning('no_recommends', 'go_back', 'javascript:history.go(-1);', 'set_recommend', 'index.php?app=recommend');
                return;
            }
            $this->assign('recommends', $recommends);
            $this->display('goods.batch.html');
        }
        else
        {
            $id = isset($_POST['id']) ? trim($_POST['id']) : '';
            if (!$id)
            {
                $this->show_warning('Hacking Attempt');
                return;
            }

            $recom_id = empty($_POST['recom_id']) ? 0 : intval($_POST['recom_id']);
            if (!$recom_id)
            {
                $this->show_warning('recommend_required');
                return;
            }

            $ids = explode(',', $id);
            $recom_mod =& bm('recommend', array('_store_id' => 0));
            $recom_mod->createRelation('recommend_goods', $recom_id, $ids);
            $ret_page = isset($_GET['ret_page']) ? intval($_GET['ret_page']) : 1;
            $this->show_message('recommend_ok','back_list','index.php?app=goods&page='.$ret_page,'view_recommended_goods','index.php?app=recommend&amp;act=view_goods&amp;id='.$recom_id);
        }
    }

    /* 编辑商品 */
    function edit()
    {
        if (!IS_POST)
        {
            // 第一级分类
            $cate_mod =& bm('gcategory', array('_store_id' => 0));
            $this->assign('gcategories', $cate_mod->get_options(0, true));

            $this->headtag('<script type="text/javascript" src="{lib file=mlselection.js}"></script>');
            $this->display('goods.batch.html');
        }
        else
        {
            $id = isset($_POST['id']) ? trim($_POST['id']) : '';
            if (!$id)
            {
                $this->show_warning('Hacking Attempt');
                return;
            }

            $ids = explode(',', $id);
            $data = array();
            if ($_POST['cate_id'] > 0)
            {
                $data['cate_id'] = $_POST['cate_id'];
                $data['cate_name'] = $_POST['cate_name'];
            }
            if (trim($_POST['brand']))
            {
                $data['brand'] = trim($_POST['brand']);
            }
            if (trim($_POST['label']))
            {
                $data['label'] = trim($_POST['label']);
            }
            if ($_POST['closed'] >= 0)
            {
                $data['closed'] = $_POST['closed'] ? 1 : 0;
                $data['close_reason'] = $_POST['closed'] ? $_POST['close_reason'] : '';
            }
            if ($_POST['wish_close'] || $_POST['wish_close'] == 0)
            {
                $data['wish_close'] = $_POST['wish_close'];
            }
            if ($_POST['sales'] || $_POST['sales'] == 0)
            {
                $data['sales'] = $_POST['sales'];
            }
            if ($_POST['discount'] || $_POST['discount'] == 0)
            {
                if(is_numeric($_POST['discount']) && $_POST['discount'] <= 1){
                    $data['discount'] = $_POST['discount'];
                }
            }


            if (empty($data))
            {
                $this->show_warning('no_change_set');
                return;
            }
            $this->admin_log(Lang::get('edit_goods'),1);
            $this->_goods_mod->edit($ids, $data);
            $ret_page = isset($_GET['ret_page']) ? intval($_GET['ret_page']) : 1;
            $this->show_message('edit_ok',
                'back_list', 'index.php?app=goods&page=' . $ret_page);
        }
    }

    //异步修改数据
   function ajax_col()
   {
       $id     = empty($_GET['id']) ? 0 : intval($_GET['id']);
       $column = empty($_GET['column']) ? '' : trim($_GET['column']);
       $value  = isset($_GET['value']) ? trim($_GET['value']) : '';
       $data   = array();

       if (in_array($column ,array('goods_name', 'brand', 'closed','mall_recommended','mall_sort_order','if_show','discount')))
       {
           $data[$column] = $value;
           dump($data);
           $this->_goods_mod->edit($id, $data);
           if(!$this->_goods_mod->has_error())
           {
               echo ecm_json_encode(true);
           }
       }
       else
       {
           return ;
       }
       return ;
   }

    /* 删除商品 */
    function drop()
    {
        if (!IS_POST)
        {
            $this->display('goods.batch.html');
        }
        else
        {
            $id = isset($_POST['id']) ? trim($_POST['id']) : '';
            if (!$id)
            {
                $this->show_warning('Hacking Attempt');
                return;
            }
            $ids = explode(',', $id);

            // notify store owner
            $ms =& ms();
            $goods_list = $this->_goods_mod->find(array(
                "conditions" => $ids,
                "fields" => "goods_name, store_id",
            ));
            foreach ($goods_list as $goods)
            {
                //$content = sprintf(LANG::get('toseller_goods_droped_notify'), );
                $content = get_msg('toseller_goods_droped_notify', array('reason' => trim($_POST['drop_reason']),
                    'goods_name' => addslashes($goods['goods_name'])));
                $ms->pm->send(MSG_SYSTEM, $goods['store_id'], '', $content);
            }
            $this->admin_log(Lang::get('del_goods'),1);
            // drop
            $this->_goods_mod->drop_data($ids);
            $this->_goods_mod->drop($ids);
            $ret_page = isset($_GET['ret_page']) ? intval($_GET['ret_page']) : 1;
            $this->show_message('drop_ok',
                'back_list', 'index.php?app=goods&page=' . $ret_page);
        }
    }

    //小程序码页面
    function appletCode(){
        $id = isset($_GET['id']) ? trim($_GET['id']) : '';
        if(!$id){
            $this->show_warning('参数错误！');
            return false;
        }

        $goodsInfo = $this->_goods_mod->get(array("conditions"=>"goods_id = ". $id));
        if(!$goodsInfo){
            $this->show_warning('商品信息错误！');
            return false;
        }
        if($goodsInfo['applet_code']){
            $goodsInfo['applet_code'] = WEB_PHOTO_SRC.'/'.$goodsInfo['applet_code'];
        }
        $this->assign('goodsInfo',$goodsInfo);
        $this->display('goods.appletCode.html');
    }
    //获取小程序码接口
    function getAppletCode(){
        $goods_id = $_POST['goods_id'];
        $code = -1;
        $msg = '参数错误';
        if($goods_id){

            $file_name = $goods_id.rand(100,999).time().'.png';
            $file = '../data/files/wx_applet/goods_'.$goods_id.'/'.$file_name;
            $appid = WX_APPLET_APPID;
            $secret = WX_APPLET_APPSECRET;

            $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
            // get请求获取access_token
            $data = $this->getCurl($url);

            $data = json_decode($data,true);
            $msg = '获取微信access_token失败';
            if($data['access_token']){
                //获取二维码
                //参数
                $postdata['scene']='goodsid='.$goods_id;
                // 宽度
                $postdata['width']=1280;
                // 页面
                $postdata['page']='pages/product/detail';
                // 线条颜色
                $postdata['auto_color']=false;
                //auto_color 为 false 时生效
                $postdata['line_color']=array('r'=>'0','g'=>'0','b'=>'0');
                // 是否有底色为true时是透明的
                $postdata['is_hyaline']=false;
                $post_data = json_encode($postdata);
                // 获取二维码
                $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$data['access_token'];
                // post请求
                $result = $this->postCurl($url,$post_data);
                // 保存二维码
                $path_log = '../data/files/wx_applet/goods_'.$goods_id.'/';
                $this->mkdirs_1($path_log, 0777, true);
                file_put_contents($file,$result);
                //上传到oss
                $pash = 'data/files/wx_applet/goods_'.$goods_id.'/'.$file_name;
                $oss = $this->_upload($pash);

                $goodsModel = &m('goods');
                $save = $goodsModel->edit($goods_id,array('applet_code'=>$pash));
                $msg = '添加失败';
                if($save){
                    $code = 1;
                    $msg = '添加成功';
                }
            }
        }
        echo json_encode(array('code'=>$code,'msg'=>$msg));
        exit();
    }

    function _upload($pash){
        require_once '../includes/aliyunOss/samples/Common.php';
        $ossClient = new \common;
        $r = $ossClient->putObjectFile('../'.$pash,$pash);
        if(!$r['info'] || $r['info']['http_code'] != 200){
            return false;
        }
        unlink('../'.$pash);
        return true;
    }

    function mkdirs_1($path, $mode = 0777){
        if(is_dir($path)){
            return '无法创建,已经是目录了';
        }else{
            if(mkdir($path, $mode, true)) {
                return '创建成功';
            }else{
                return '创建失败';
            }
        }
    }
}

?>
